package cn.pa4j.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.setting.Setting;
import cn.pa4j.entity.Live;
import cn.pa4j.entity.TextMsg;
import cn.pa4j.entity.WxMessage;
import cn.pa4j.service.LiveNotificationService;
import cn.pa4j.utils.WxUtil;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.binding.MapperProxy;
import org.apache.ibatis.binding.MapperProxyFactory;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController()
public class WxController {

    @Autowired
    LiveNotificationService service;

    Logger log = LoggerFactory.getLogger("bussiness");


    /**
     * 校验微信token或校验服务器是否在线
     * @return
     */
    @GetMapping("wx")
    public String main(HttpServletRequest request){
        if (WxUtil.checkIsWx(request,WxUtil.setting.getStr("token"))){
            return request.getParameter("echostr");
        }else {
            return DateUtil.now();
        }
    }

    /**
     * 接收处理微信消息
     * @return
     */
    @PostMapping("wx")
    public String onMessage(HttpServletRequest request) throws Exception {
        System.out.println("message");
        WxMessage message = new WxMessage(request);
        String content = message.getContent();
        String userId = message.getFromUserName();
        System.out.println(userId);
        if(content==null){
            return null;
        }
        if ("直播通知".equals(content)){
            String msg = service.getLives(userId);
            return new TextMsg(message,msg).toMessage();
        }else if (content.startsWith("订阅 ")){
            String msg;
            try {
                String[] strings = StrUtil.split(content, " ");
                msg = service.subscribe(userId,Integer.valueOf(strings[1]).toString());
            }catch (Exception e){
                msg = "格式错误";
            }
            return new TextMsg(message,msg).toMessage();
        }else if (content.startsWith("取消订阅 ")){
            String msg;
            try {
                String[] strings = StrUtil.split(content, " ");
                msg = service.unsubscribe(userId,Integer.valueOf(strings[1]).toString());
            }catch (Exception e){
                msg = "格式错误";
            }
            return new TextMsg(message,msg).toMessage();
        }else {
            return new TextMsg(message,"有事吗？").toMessage();
        }
    }

    @RequestMapping("test")
    public String test(String knock) {
        log.info("String knock:" + knock);
        return "123";
    }

    @RequestMapping("test2")
    public String test2(String knock) {
        return "123";
    }


}
